{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Random Dot Product Graph (RDPG) Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import graspologic\n",
    "\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "RDPG is a latent position generative model, in which the probability of an edge existing between pairs of vertices is determined by the dot product of the associated latent position vectors. In other words, given $X \\in \\mathbb{R}^{n\\times d}$, where $n$ is the number of vertices and $d$ is the dimensionality of each vector, the probability matrix $P$ is given by:\n",
    "\n",
    "$$ P = X X^T $$\n",
    "\n",
    "Both ER and SBM models can be formulated as a RDPG. Below, we sample $ER_{NP}(100, 0.5)$ using RDPG formulation. In this case, we set $X \\in \\mathbb{R}^{100\\times 2}$ where all the values in $X$ is 0.5. This results in $P$ matrix where all the probabilities are also 0.5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from graspologic.simulations import rdpg\n",
    "\n",
    "# Create a latent position matrix\n",
    "X = np.full((100, 2), 0.5)\n",
    "print(X @ X.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = rdpg(X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize the adjacency matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graspologic.plot import heatmap\n",
    "\n",
    "_ = heatmap(A, title='ER_NP(100, 0.5) Using RDPG')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stochastic block model as RDPG\n",
    "\n",
    "We can formulate the following 2-block SBM parameters as RDPG, where the latent positions live in $\\mathbb{R}^3$.\n",
    "\n",
    "\\begin{align*}\n",
    "n &= [50, 50]\\\\\n",
    "p &= \\begin{bmatrix}0.33 & 0.09\\\\\n",
    "0.09 & 0.03\n",
    "\\end{bmatrix}\n",
    "\\end{align*}\n",
    "\n",
    "as\n",
    "\n",
    "\\begin{align*}\n",
    "X &= \\begin{bmatrix}0.5 & 0.2 & 0.2\\\\\n",
    "& \\vdots & \\\\\n",
    "0.1 & 0.1 & 0.1\\\\\n",
    "& \\vdots & \n",
    "\\end{bmatrix}\\\\\n",
    "P &= XX^T\n",
    "\\end{align*}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[0.5, 0.2, 0.2]] * 50 + [[0.1, 0.1, 0.1]] * 50)\n",
    "A_rdpg = rdpg(X, loops=False)\n",
    "_ = heatmap(A_rdpg, title='2-block SBM as RDPG')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Results from SBM simulation using same formulation shows similar structure"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graspologic.simulations import sbm\n",
    "\n",
    "n = [50, 50]\n",
    "p = [[0.33, 0.09], [0.09, 0.03]]\n",
    "\n",
    "A_sbm = sbm(n, p)\n",
    "_ = heatmap(A_sbm, title = 'SBM Simulation')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
